package com.fafa.yygh.cmn.controller;

import com.fafa.yygh.cmn.service.DictService;
import com.fafa.yygh.common.result.Result;
import com.fafa.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Sire
 * @version 1.0
 * @date 2022-02-08 15:11
 */
@Api(tags = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {

    @Autowired
    private DictService dictService;

    /**
     * 根据数据id查询子数据列表
     */
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping(value = "/findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        List<Dict> childData = dictService.findChildData(id);
        return Result.ok(childData);
    }

    /**
     * 导出数据字典
     *
     * @param response
     */
    @ApiOperation(value = "导出")
    @GetMapping("/exportData")
    public void exportDict(HttpServletResponse response) {
        dictService.exportData(response);
    }

    /**
     * 导入数据字典
     *
     * @param file
     */
    @ApiOperation(value = "导入")
    @PostMapping(value = "/importData")
    public Result importDict(MultipartFile file) {
        dictService.importData(file);
        return Result.ok();
    }

    /**
     * 获取数据字典名称（需要上级编码）
     *
     * @param parentDictCode
     * @param value
     * @return
     */
    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(value = "/getName/{parentDictCode}/{value}")
    public String getName(@ApiParam(name = "parentDictCode", value = "上级编码", required = true)
                          @PathVariable("parentDictCode") String parentDictCode,
                          @ApiParam(name = "value", value = "值", required = true)
                          @PathVariable("value") String value) {
        String name = dictService.getNameByParentDictCodeAndValue(parentDictCode, value);
        return name;
    }

    /**
     * 获取数据字典名称（不需要上级编码）
     *
     * @param value
     * @return
     */
    @ApiImplicitParam(name = "value", value = "值", required = true, dataType = "Long", paramType = "path")
    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(value = "/getName/{value}")
    public String getName(@ApiParam(name = "value", value = "值", required = true)
                          @PathVariable("value") String value) {
        String name = dictService.getNameByParentDictCodeAndValue("", value);
        return name;
    }

    /**
     * 根据dictCode获取下级节点
     *
     * @param dictCode
     * @return
     */
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(@ApiParam(name = "dictCode", value = "节点编码", required = true)
                                             @PathVariable String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }

}
